<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Wrapper library</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Wrapper_library"> <link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Wrapper_library rootpage-Wrapper_library skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Wrapper library</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<p><b>Wrapper libraries</b> (or <b>library wrappers</b>) consist of a thin layer of code (a "<a href="Shim_(computing)" title="Shim (computing)">shim</a>") which translates a <a href="Library_(computing)" title="Library (computing)">library</a>'s existing interface into a compatible interface. This is done for several reasons:
</p>
<ul><li>To refine a poorly designed or complicated interface</li>
<li>Allow code to work together which otherwise cannot (e.g. incompatible data formats)</li>
<li>Enable cross language and/or <a href="Run-time_system" class="mw-redirect" title="Run-time system">runtime</a> interoperability</li></ul>
<p>Wrapper libraries can be implemented using the <a href="Adapter_pattern" title="Adapter pattern">adapter</a>, <a href="Facade_pattern" title="Facade pattern">façade</a>, and to a lesser extent, <a href="Proxy_pattern" title="Proxy pattern">proxy</a> <a href="Design_pattern_(computer_science)" class="mw-redirect" title="Design pattern (computer science)">design patterns</a>.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Structure_and_implementation">Structure and implementation</h2></div>
<p>The specific way in which a wrapper library is implemented is highly specific to the environment it is being written in and the scenarios which it intends to address. This is especially true in the case when <a href="#Cross-language.2Fruntime_interoperability">cross-language/runtime interoperability</a> is a consideration.
</p>
<div class="mw-heading mw-heading3"><h3 id="Example">Example</h3></div>
<p>The following provides a general illustration of a common wrapper library implementation. In this example, a <a href="C%2B%2B" title="C++">C++</a> interface acts as a "wrapper" around a <a href="C_(programming_language)" title="C (programming language)">C</a> interface.
</p>
<div class="mw-heading mw-heading4"><h4 id="C_interface">C interface</h4></div>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="kt">int</span><span class="w"> </span><span class="nf">pthread_mutex_init</span><span class="p">(</span><span class="n">pthread_mutex_t</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">mutex</span><span class="w"> </span><span class="p">,</span><span class="w"> </span><span class="n">pthread_mutexattr_t</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">attr</span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">pthread_mutex_destroy</span><span class="w"> </span><span class="p">(</span><span class="n">pthread_mutex_t</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">mutex</span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">pthread_mutex_lock</span><span class="w"> </span><span class="p">(</span><span class="n">pthread_mutex_t</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">mutex</span><span class="w"> </span><span class="p">);</span>
<span class="kt">int</span><span class="w"> </span><span class="nf">pthread_mutex_unlock</span><span class="w"> </span><span class="p">(</span><span class="n">pthread_mutex_t</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">mutex</span><span class="w"> </span><span class="p">);</span>
</pre></div>
<div class="mw-heading mw-heading4"><h4 id="C++_wrapper">C++ wrapper</h4></div>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="k">class</span><span class="w"> </span><span class="nc">Mutex</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">pthread_mutex_t</span><span class="w"> </span><span class="n">mutex</span><span class="p">;</span>
<span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="n">Mutex</span><span class="p">()</span><span class="w"> </span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">pthread_mutex_init</span><span class="p">(</span><span class="o">&</span><span class="n">mutex</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="o">~</span><span class="n">Mutex</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">pthread_mutex_destroy</span><span class="p">(</span><span class="o">&</span><span class="n">mutex</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="k">friend</span><span class="w"> </span><span class="k">class</span><span class="w"> </span><span class="nc">Lock</span><span class="p">;</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">lock</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">pthread_mutex_lock</span><span class="p">(</span><span class="o">&</span><span class="n">mutex</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">unlock</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">pthread_mutex_unlock</span><span class="p">(</span><span class="o">&</span><span class="n">mutex</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">};</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Lock</span>
<span class="p">{</span>
<span class="k">private</span><span class="o">:</span>
<span class="w"> </span><span class="n">Mutex</span><span class="w"> </span><span class="o">&</span><span class="n">mutex</span><span class="p">;</span>
<span class="k">public</span><span class="o">:</span>
<span class="w"> </span><span class="n">Lock</span><span class="p">(</span><span class="n">Mutex</span><span class="w"> </span><span class="o">&</span><span class="n">mutex</span><span class="p">)</span><span class="o">:</span><span class="w"> </span><span class="n">mutex</span><span class="p">{</span><span class="n">mutex</span><span class="p">}</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">mutex</span><span class="p">.</span><span class="n">lock</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="o">~</span><span class="n">Lock</span><span class="p">()</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">mutex</span><span class="p">.</span><span class="n">unlock</span><span class="p">();</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">};</span>
</pre></div>
<p>The original C interface can be regarded as error prone, particularly in the case where users of the library forget to unlock an already locked mutex. The new interface effectively utilizes <a href="Resource_acquisition_is_initialization" title="Resource acquisition is initialization">resource acquisition is initialization</a> (RAII) in the new <style data-mw-deduplicate="TemplateStyles:r886049734">
/* start https://en.wikipedia.org/ */
.mw-parser-output .monospaced{font-family:monospace,monospace}
/* end https://en.wikipedia.org/ */
</style><span class="monospaced">Mutex</span> and <span class="monospaced">Lock</span> classes to ensure <span class="monospaced">Mutex</span>s are eventually unlocked and <span class="monospaced">pthread_mutex_t</span> objects are automatically released.
</p><p>The above code closely mimics the implementation of <span class="monospaced">boost::scoped_lock</span> and <span class="monospaced">boost::mutex</span> which are part of the <a rel="nofollow" class="external text" href="http://www.boost.org/doc/libs/1_38_0/doc/html/thread.html">boost::thread</a> library.
</p>
<div class="mw-heading mw-heading3"><h3 id="Driver_wrappers">Driver wrappers</h3></div>
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */
.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}
/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Further information: <a href="Driver_wrapper" title="Driver wrapper">Driver wrapper</a></div>
<div class="mw-heading mw-heading2"><h2 id="Cross-language/runtime_interoperability">Cross-language/runtime interoperability</h2></div>
<p>Some wrapper libraries exist to act as a bridge between a client application and a library written using an incompatible technology. For instance, a <a href="Java_(programming_language)" title="Java (programming language)">Java</a> application may need to execute a <a href="System_call" title="System call">system call</a>. However system calls are typically exposed as C library functions. To resolve this issue Java implements wrapper libraries which make these system calls callable from a Java application.
</p><p>In order to achieve this, languages like Java provide a mechanism called <a href="Foreign_function_interface" title="Foreign function interface">foreign function interface</a> that makes this possible. Some examples of these mechanisms include:
</p>
<ul><li><a href="Java_Native_Interface" title="Java Native Interface">Java Native Interface (JNI)</a></li>
<li><a href="Java_Native_Access" title="Java Native Access">Java Native Access (JNA)</a></li>
<li><a rel="nofollow" class="external text" href="https://docs.python.org/library/ctypes.html">A foreign function library for Python</a></li>
<li><a href="Managed_Extensions_for_C%2B%2B" title="Managed Extensions for C++">Managed Extensions</a></li>
<li><a href="SWIG" title="SWIG">SWIG (Simplified Wrapper and Interface Generator)</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="Existing_wrapper_libraries">Existing wrapper libraries</h2></div>
<p>Some examples of existing wrapper libraries:
</p>
<ul><li><a rel="nofollow" class="external text" href="http://sourceware.org/pthreads-win32/">Pthreads for WIN32</a></li>
<li><a rel="nofollow" class="external text" href="https://pyopengl.sourceforge.net/">OpenGL Bindings for Python</a></li>
<li><a rel="nofollow" class="external text" href="http://www.tangentsoft.net/mysql++/">MySQL++</a></li>
<li><a rel="nofollow" class="external text" href="https://github.com/bytedeco/javacv/">JavaCV</a></li>
<li><a href="WineD3D" class="mw-redirect" title="WineD3D">WineD3D</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Wrapper_function" title="Wrapper function">Wrapper function</a></li>
<li><a href="Wrapper_pattern" class="mw-redirect" title="Wrapper pattern">Wrapper pattern</a></li>
<li><a href="Glue_code" title="Glue code">Glue code</a></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-04-23" href="https://en.wikipedia.org/wiki/?title=Wrapper_library&oldid=1287021467">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>